package com.jsd2412.backend.turnRoom.service.impl;

import com.jsd2412.backend.bed.mapper.BedMapper;
import com.jsd2412.backend.bed.pojo.dto.BedQuery;
import com.jsd2412.backend.bed.pojo.vo.BedVO;
import com.jsd2412.backend.room.mapper.RoomMapper;
import com.jsd2412.backend.turnRoom.pojo.dto.TurnRoomAddParam;
import com.jsd2412.backend.turnRoom.mapper.TurnRoomMapper;
import com.jsd2412.backend.turnRoom.pojo.dto.TurnRoomQuery;
import com.jsd2412.backend.turnRoom.pojo.entity.TurnRoom;
import com.jsd2412.backend.turnRoom.pojo.vo.TurnRoomVO;
import com.jsd2412.backend.turnRoom.service.TurnRoomService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class TurnRoomServiceImpl implements TurnRoomService {
    @Autowired
    private TurnRoomMapper turnRoomMapper;
    @Autowired
    private RoomMapper roomMapper;
    @Autowired
    private BedMapper bedMapper;

    @Override
    public void addTurnRoom(TurnRoomAddParam addParam) {
        log.debug("添加转房记录业务:{}", addParam);

        TurnRoom turnRoom = new TurnRoom();
        turnRoom.setPatientId(addParam.getId());
        turnRoom.setReason(addParam.getReason());
        turnRoom.setCreateTime(new Date());

        //获取旧房间id
        Integer originRoomId = roomMapper.selectByNumber(addParam.getRoomNumber()).getId();
        turnRoom.setOriginRoomId(originRoomId);
        //获取新房间id
        Integer newRoomId = roomMapper.selectByNumber(addParam.getNewRoomNumber()).getId();
        turnRoom.setNewRoomId(newRoomId);

        BedQuery bedQuery = new BedQuery();
        //获取旧床位id
        bedQuery.setRoomId(originRoomId);
        bedQuery.setNumber(addParam.getBedNumber());
        Integer originBedId = bedMapper.select(bedQuery).get(0).getBedId();
        turnRoom.setOriginBedId(originBedId);
        //获取新床位id
        bedQuery.setRoomId(newRoomId);
        bedQuery.setNumber(addParam.getNewBedNumber());
        Integer newBedId = bedMapper.select(bedQuery).get(0).getBedId();
        turnRoom.setNewBedId(newBedId);

        turnRoomMapper.insert(turnRoom);
    }

    @Override
    public List<TurnRoomVO> selectTurnRoom(TurnRoomQuery query) {
        log.debug("查询转房记录业务:{}", query);
        List<TurnRoomVO> list = turnRoomMapper.selectTurnRoom(query);
        for (TurnRoomVO turnRoomVO : list) {
            //获取旧房号
            String originRoomNumber = roomMapper.selectById(turnRoomVO.getOriginRoomId()).getNumber();
            turnRoomVO.setRoomNumber(originRoomNumber);
            //获取新房号
            String newRoomNumber = roomMapper.selectById(turnRoomVO.getNewRoomId()).getNumber();
            turnRoomVO.setNewRoomNumber(newRoomNumber);
            //获取原床位的床号和金额
            BedVO originBedVO = bedMapper.selectById(turnRoomVO.getOriginBedId());
            if(originBedVO!=null){
                turnRoomVO.setOriginBed(originBedVO.getNumber());
                turnRoomVO.setOriginBedPrice(originBedVO.getPrice());
            }
            //获取新床位的床号和金额
            BedVO newBedVO = bedMapper.selectById(turnRoomVO.getNewBedId());
            if(newBedVO!=null){
                turnRoomVO.setNewBed(newBedVO.getNumber());
                turnRoomVO.setNewBedPrice(newBedVO.getPrice());
            }
        }
        return list;
    }
}
